﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using EasyModbus;

namespace Liju.Common
{
    /// <summary>
    /// IO控制器 Wlan口
    /// </summary>
    class ModbusIO
    {
        private readonly object serialLock = new object();

        private ModbusClient modbusClient; // 定义ModbusClient对象

        public bool IOConState;

        /// <summary>
        /// 1汇川 2三菱
        /// </summary>
        int PLCType = 2;

        public ModbusIO(string IpAddress, int Port)
        {
            lock (serialLock)
            {
                try
                {
                    modbusClient = new ModbusClient(IpAddress, Convert.ToInt32(Port)); // 填写威纶通屏幕的IP地址和端口号
                    modbusClient.Connect();

                    if (modbusClient.Connected)
                    {
                        IOConState = true;
                    }
                    else
                    {
                        IOConState = false;
                    }
                }
                catch (Exception)
                {

                    IOConState = false;
                }
            }
        }

        ///关闭连接 
        public void Close()
        {
            lock (serialLock)
            {
                if (modbusClient.Connected == true)
                {
                    modbusClient.Disconnect();
                }
            }
        }

        /// <summary>
        /// IO输出-关
        /// </summary>
        /// <param name="Number">寄存器地址最大16位</param>
        /// <returns></returns>
        public bool SetOutClose(int Number)
        {
            try
            {
                //三菱
                //Number = Number + 0;                
                if (PLCType == 1)//汇川
                {
                    Number = Number + 64512;
                }
                lock (serialLock)
                {
                    modbusClient.WriteSingleCoil(Number, false);
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// IO输出-开
        /// </summary>
        /// <param name="Number">寄存器地址最大16位</param>
        /// <returns></returns>
        public bool SetOutOpen(int Number)
        {
            try
            {
                //三菱
                //Number = Number + 0;                
                if (PLCType == 1)//汇川
                {
                    Number = Number + 64512;
                }
                lock (serialLock)
                {
                    modbusClient.WriteSingleCoil(Number, true);
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool[] GetInValueAll()
        {
            try
            {
                lock (serialLock)
                {
                    if (PLCType == 1)//汇川
                    {
                        bool[] bol = modbusClient.ReadCoils(63488, 24);
                        return bol;
                    }
                    else//三菱PLC M0-M23对应X0-X7 X10-X17 X20-X27 X30-X37 X40-X47 X50-X57拓展至48个输入点
                    {
                        bool[] bol = modbusClient.ReadCoils(8192, 48);
                        return bol;
                    }
                }
            }
            catch (Exception)
            {
                bool[] bol = new bool[1];
                return bol;
            }
        }

    }
}
